<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\ContactTransfer\Action\Sidebar;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use MoChat\App\Common\Middleware\SidebarAuthMiddleware;
use MoChat\App\Corp\Logic\AppTrait;
use MoChat\App\WorkContact\Contract\WorkContactContract;
use MoChat\App\WorkEmployee\Contract\WorkEmployeeContract;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Plugin\ContactTransfer\Contract\WorkTransferLogContract;

/**
 * 客户画像-转接客户.
 * @Controller
 */
class Transfer extends AbstractAction
{
    use ValidateSceneTrait;
    use AppTrait;

    /**
     * @Inject
     * @var WorkEmployeeContract
     */
    protected $workEmployeeService;

    /**
     * @Inject
     * @var WorkContactContract
     */
    protected $workContactService;

    /**
     * @Inject
     * @var WorkTransferLogContract
     */
    protected $workTransferLogService;

    /**
     * @Inject
     * @var RequestInterface
     */
    protected $request;

    /**
     * 客户转接接口.
     * @Middlewares({
     *     @Middleware(SidebarAuthMiddleware::class)
     * })
     * @RequestMapping(path="/sidebar/contactTransfer/transfer", methods="POST")
     */
    public function handle(): array
    {
        // 接收参数
        $params = [
            'contactId' => (int) $this->request->input('contactId'),
            'employeeId' => (int) $this->request->input('employeeId'),
            'content' => $this->request->input('content'),
        ];
        // 校验参数
        $this->validated($params);
        $handoverEmployee = $this->workEmployeeService->getWorkEmployeeById((int) user()['id'], ['wx_user_id']);
        $takeoverEmployee = $this->workEmployeeService->getWorkEmployeeById($params['employeeId'], ['name', 'wx_user_id']);
        $contact = $this->workContactService->getWorkContactById($params['contactId'], ['name', 'wx_external_userid']);
        $wx = $this->wxApp(user()['corpIds'][0], 'contact')->external_contact;
        $temp = $wx->transferCustomer([$contact['wxExternalUserid']], $handoverEmployee['wxUserId'], $takeoverEmployee['wxUserId'], $params['content']);
        $result[] = $temp;
        if ($temp['errcode'] === 0) {
            $this->workTransferLogService->createWorkTransferLog([
                'corp_id' => user()['corpIds'][0],
                'status' => 2,
                'type' => 1,
                'name' => $contact['name'],
                'contact_id' => $contact['wxExternalUserid'],
                'handover_employee_id' => $handoverEmployee['wxUserId'],
                'takeover_employee_id' => $takeoverEmployee['wxUserId'],
            ]);
        }
        return $result;
    }

    /**
     * @return string[] 规则
     */
    public function rules(): array
    {
        return [
            'employeeId' => 'required|integer|min:1|bail',
            'contactId' => 'required|integer|min:1|bail',
            'content' => 'required',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    public function messages(): array
    {
        return [
            'employeeId.required' => '员工id 必传',
            'contactId.required' => '客户id 必传',
            'content.required' => '内容 必传',
        ];
    }
}
